<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "product".
 *
 * @property integer $id
 * @property string $name
 * @property integer $designer_id
 * @property integer $user_id
 * @property string $description
 * @property double $material_cost
 * @property boolean $is_material
 * @property string $appendix
 * @property boolean $status
 * @property string $pass_time
 * @property string $govern_opinion
 * @property string $create_at
 * @property string $update_at
 *
 * @property Order[] $orders
 * @property Member $designer
 * @property User $user
 * @property ProductMaterial[] $productMaterials
 * @property UserProduct[] $userProducts
 */
class Product extends \app\models\BaseModel {
    const STATUS_CREATE = 1;
    const STATUS_AUTH = 2;
    const STATUS_REJECT = 3;
    public static $positions = [
        self::STATUS_CREATE => '待审核',
        self::STATUS_AUTH => '已通过',
        self::STATUS_REJECT => '已拒绝',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'product';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['name', 'designer_id', 'user_id', 'description', 'material_cost'], 'required'],
            [['designer_id', 'user_id', 'status', ], 'integer'],
            [['is_material', ], 'boolean'],
            [['material_cost'], 'number'],
            [['create_at', 'update_at'], 'safe'],
            [['name', 'description', 'appendix', 'pass_time', 'govern_opinion'], 'string', 'max' => 255],
            [['designer_id'], 'exist', 'skipOnError' => true, 'targetClass' => Member::className(), 'targetAttribute' => ['designer_id' => 'id']],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => '产品ID',
            'name' => '产品名称',
            'designer_id' => '设计师',
            'user_id' => '所属公司',
            'description' => '产品描述',
            'material_cost' => '产品的原料成本',
            'is_material' => '产品类型（只有两个值：原料型产品/非原料型产品）',
            'appendix' => '附件',
            'status' => '是否过审',
            'pass_time' => '政府审核通过时间（第x财年）',
            'govern_opinion' => '政府意见',
            'create_at' => '创建时间',
            'update_at' => '更新时间',
        ];
    }

    public function fields() {
        $fields = [
            'id',
            'name',
            'designer',
            'user',
            'description',
            'material_cost',
            'is_material',
            'appendix',
            'status',
            'pass_time',
            'govern_opinion',
            'productMaterials',
        ];
        $fields = array_combine($fields, $fields);

        return $fields;
    }

    public function extraFields() {
        $fields = [
            'designer_id',
            'user_id',
            'create_at',
            'update_at',
        ];
        $fields = array_combine($fields, $fields);
        return $fields;
    }

    public function getOrders() {
        return $this->hasMany(Order::className(), ['product_id' => 'id'])->inverseOf('product');
    }

    public function getDesigner() {
        return $this->hasOne(Member::className(), ['id' => 'designer_id'])->inverseOf('products');
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'user_id'])->inverseOf('products');
    }

    public function getProductMaterials() {
        return $this->hasMany(ProductMaterial::className(), ['product_id' => 'id'])->inverseOf('product');
    }

    public function getUserProducts() {
        return $this->hasMany(UserProduct::className(), ['product_id' => 'id'])->inverseOf('product');
    }
}
